<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="#/home">首页</a>
    <a href="#/list">列表</a>
    <a href="#/app">应用</a>

    <div id="box"></div>

    <script>
        class Router{
            constructor(props) {
                this.routes = props.routes
                this.box = document.getElementById('box')
                this.event = {}

                this.init()
            }
            init() {
                this.routes.forEach(item => {
                    this.event[item.path] = () => {
                        this.box.innerHTML = item.com
                    }
                })

                // 1. 添加hashchange事件
                // 2. 获取url上#后的内容
                // 3. 给event设置 key(url)  value(设置#box内容)
                // 4. 调用函数
                window.addEventListener('hashchange', () => {
                    let path = location.hash.slice(1)
                    this.event[path]()
                })
            }
        }

        const routes = [
            {
                path: "/home",
                com: "我是首页"
            },
            {
                path: "/list",
                com: "我是列表"
            },
            {
                path: "/app",
                com: "我是应用"
            }
        ]

        let router = new Router({
            routes
        })
    </script>
</body>
</html>